package xm.components.checkbox
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import xm.core.XMSprite;
	import xm.core.XprimeMobileGlobals;
	import xm.definition.Align;
	import xm.manager.LayoutMananger;
	
	
	/**
	 * <br> 클래스 설명 : 체크박스 버튼 </br>
	 * @author 김정환
	 */	
	public class CheckBox extends XMSprite
	{
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * <br>Align.RIGHT(default) , Align.LEFT</br>
		 * <br>레이블과 버튼의 정렬.. 우측정렬인가 좌측정렬인가 </br> 
		 */		
		public var alignLabel : String = "right";
		
		/**
		 * <br>버튼과 레이블 사이의 간격 ( default = 5 )</br> 
		 */		
		public var gapBetweenBtnAndLabel : Number = 5;
		
		/**
		 * <br>기본스킨 ( default = 5 )</br> 
		 */		
		public var defaultCheckBoxSkin : MovieClip;
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _skin : MovieClip;
		private var _labelTF : TextField;
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function CheckBox()
		{
			trace("===> CheckBox , CheckBox : " );
			if( XprimeMobileGlobals.layoutManager == null ) 
			{
				XprimeMobileGlobals.layoutManager = new LayoutMananger( this );
			}
			super();
		}

		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
		}
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			var wid : Number = 0, hei : Number = 0;
			
			if( _labelTF )
			{
				this.setChildIndex( _labelTF, numChildren-1 );
				_labelTF.width = _labelTF.textWidth + 4;
				_labelTF.height = _labelTF.textHeight + 4;
				
				skin.y = paddingTop;
					
				if( alignLabel == Align.RIGHT )
				{
					skin.x = paddingLeft;
					_labelTF.x = skin.x + skin.width + gapBetweenBtnAndLabel;
					
					wid = _labelTF.x + _labelTF.width;
				}
				else
				{
					_labelTF.x = paddingLeft;
					skin.x = _labelTF.x + _labelTF.width + gapBetweenBtnAndLabel;
					
					wid = skin.x + skin.width;
				}
				
				_labelTF.y = skin.y + ( skin.height - _labelTF.height )/2;
				
				hei = Math.max( skin.y+skin.height, _labelTF.y+_labelTF.height );
			}
			else
			{
				skin.x = paddingLeft;
				skin.y = paddingTop;
				
				wid = skin.x + skin.width;
				hei = skin.y+skin.height;
			}
			
			
			this.viewWidth = wid;
			this.viewHeight = hei;
			
			this.backgroundAlpha = 0;
			
			super.updateDisplayList(enforce);
			
		}
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			this.addEventListener( MouseEvent.CLICK, onClick );
		}
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		
		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			alignLabel = null;
			gapBetweenBtnAndLabel = NaN;
			
			if( _labelTF && this.contains( _labelTF ) )
			{
				this.removeChild( _labelTF );
			}
			
			_labelTF = null;
			
			
			if( _skin && this.contains( _skin ) )
			{
				this.removeChild( _skin );
			}
			
			_skin = null;
			
			this.removeEventListener( MouseEvent.CLICK, onClick );
			
			super.destroy();
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return MovieClip
		 */		
		public function get skin():MovieClip
		{
			if( _skin == null ) 
			{
				if( defaultCheckBoxSkin )	_skin = defaultCheckBoxSkin;			// Design 모드 일때
				else						_skin = new XM_DEFAULT_SKIN_CHECKBOX;	// Code 모드 일때
				addChild( _skin );
			}
			
			return _skin;
		}
		
		/**
		 * <br>디폴트와 다운이 설정된 2프레임짜리 무비클립을 스킨으로 삽입해야 함.</br>
		 * @param value
		 */		
		public function set skin(value:MovieClip):void
		{
			if( _skin && this.contains( _skin ) )
			{
				this.removeChild( _skin );
				_skin = null;
			}
			
			_skin = value;
			this.addChild( _skin );
		}
		
		[Inspectable(name="label", defaultValue = "", type = String )]
		/**
		 * @return 레이블 텍스트 또는 ""; 
		 */		
		public function get label():String
		{
			return ( _labelTF ) ? _labelTF.text : ""; 
		}
		/**
		 * @param str 레이블 
		 */		
		public function set label( str : String ):void
		{
			
			if( _labelTF == null )
			{
				var tf : TextFormat;
				
				tf = new TextFormat( "돋움", 20, 0x666666, true );
				
				_labelTF = new TextField();
				_labelTF.defaultTextFormat = tf;
				_labelTF.autoSize = TextFieldAutoSize.LEFT;
				_labelTF.selectable = false;
				
				addChild( _labelTF );
			}
			
			_labelTF.text = str;
			
			updateDisplayList( true );
		}
		
		
		
		
		
		
		/**
		 * <br>value : Boolean = true - 선택 활성화</br>
		 * @param value
		 */		
		public function set selected( value : Boolean ):void
		{
			if( value )
			{
				this._skin.gotoAndStop( 2 );
			}
			else
			{
				this._skin.gotoAndStop( 1 );
			}
		}
		
		
		/**
		 * <br>선택여부</br>
		 * @return 
		 */		
		public function get selected():Boolean
		{
			if( this._skin.currentFrame == 1 )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		
		/**
		 * <br>레이블 필드 반환</br>
		 * @return 
		 */		
		public function get textField():TextField
		{
			return _labelTF;
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * @param e
		 */		
		protected function onClick(e:MouseEvent):void
		{
			selected = !selected;
		}
		
		
		
		
		
		
		
	}
}